﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace BMSQ.Utility
{
    public class UploadFiles
    {
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="fileData"></param>
        /// <param name="folder"></param>
        /// <returns></returns>
        public string UploadImg(HttpPostedFile fileData, string folder)
        {
            string fileName = "";
            string savePath = "";
            if (fileData != null)
            {
                //保存路径
                savePath = System.Web.HttpContext.Current.Server.MapPath(folder);
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }
                //生成文件名
                fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + GetFileName(fileData.FileName);
                //保存文件
                fileData.SaveAs(Path.Combine(savePath, fileName));
            }
            return fileName;
        }

        public void DeleteImage(string imagePath)
        {
            if (File.Exists(imagePath))
            {
                File.Delete(imagePath);
            }
        }

        public string GetFileName(string filefullname)
        {
            string[] file = filefullname.Split('\\');
            return file[file.Length - 1];
        }
    }
}
